package com.galengao.springboot.validateAndResponse.resp;

import com.galengao.springboot.validateAndResponse.CodeEnum;
import lombok.Data;

/**
 * 封装请求基本响应
 *
 * @author GalenGao
 * @version Id: BaseResponse.java, v 0.1 2022/5/31 9:53 GalenGao Exp $$
 */
@Data
public class BaseResponse<T> {

    /**
     * 响应结构描述
     */
    private String messages;

    /**
     * 响应状态码
     */
    private Integer code;

    /**
     * 返回的数据
     */
    private T data;


    /**
     * 响应成功的处理
     * @param data 返回实体
     * @param <T> 泛型
     * @return BaseResponse
     */
    public static <T> BaseResponse<T> success(T data) {
        BaseResponse<T> response = new BaseResponse<>();
        response.setCode(CodeEnum.SUCCESS.getCode());
        response.setMessages(CodeEnum.SUCCESS.getMessage());
        response.setData(data);
        return  response;
    }

    /**
     * 成功响应出路
     * @return BaseResponse
     */
    public static <T> BaseResponse<T> success() {
        BaseResponse<T> response = new BaseResponse<>();
        response.setCode(CodeEnum.SUCCESS.getCode());
        response.setMessages(CodeEnum.SUCCESS.getMessage());
        return  response;
    }


    /**
     * 响应失败的处理
     * @param code 错误码
     * @param messages 错误信息
     * @param <T> 泛型
     * @return BaseResponse
     */
    public static <T> BaseResponse<T> fail(String messages,Integer code) {
        BaseResponse<T> response = new BaseResponse<>();
        response.setCode(code);
        response.setMessages(messages);
        return  response;
    }
}
